package hl.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import hl.dao.BaseDao;
import hl.model.TCourseDetail;
import hl.model.TGroupCourse;
import hl.model.TTraining;
import hl.pageModel.Charts;
import hl.pageModel.CourseDetail;
import hl.pageModel.DataGrid;
import hl.service.CourseDetailService;

@Service("courseDetailService")
public class CourseDetailServiceImpl implements CourseDetailService {

	private BaseDao<TCourseDetail> courseDetailDao;

	public BaseDao<TCourseDetail> getCourseDetailDao() {
		return courseDetailDao;
	}

	@Autowired
	public void setCourseDetailDao(BaseDao<TCourseDetail> courseDetailDao) {
		this.courseDetailDao = courseDetailDao;
	}

	@Autowired
	public void setGroupCourseDao(BaseDao<TGroupCourse> groupCourseDao) {
		this.groupCourseDao = groupCourseDao;
	}

	private BaseDao<TGroupCourse> groupCourseDao;

	public BaseDao<TGroupCourse> getGroupCourseDao() {
		return groupCourseDao;
	}

	@Autowired
	public void setTrainingDao(BaseDao<TTraining> trainingDao) {
		this.trainingDao = trainingDao;
	}

	private BaseDao<TTraining> trainingDao;

	public BaseDao<TTraining> getTrainingDao() {
		return trainingDao;
	}

	@Override
	public DataGrid getDatagrid(CourseDetail courseDetail) {
		DataGrid dg = new DataGrid();
		String hql = "from TCourseDetail t ";

		Map<String, Object> params = new HashMap<String, Object>();

		hql = search(courseDetail, hql, params);

		if (courseDetail.getOrder() != null) {
			hql += " order by " + courseDetail.getSort() + " " + courseDetail.getOrder();
		}

		List<TCourseDetail> l = courseDetailDao.find(hql, params, courseDetail.getPage(), courseDetail.getRows());

		int totalHql = courseDetailDao.count(hql, params);

		List<CourseDetail> pl = new ArrayList();
		if (l != null && l.size() > 0) {
			for (TCourseDetail t : l) {
				CourseDetail u = new CourseDetail();
				BeanUtils.copyProperties(t, u);
				pl.add(u);
			}
		}
		dg.setRows(pl);
		dg.setTotal(totalHql);
		return dg;
	}

	private String search(CourseDetail courseDetail, String hql, Map<String, Object> params) {
		if (courseDetail.getName() != null && !courseDetail.getName().trim().equals("")) {
			hql += " where t.name like :value1 ";
			params.put("value1", "%" + courseDetail.getName().trim() + "%");
		}
		/*
		 * if (courseDetail.getGender() != null && !courseDetail.getGender().equals("")) { if (hql.contains("where")) hql += " and "; else hql += " where "; hql += " t.gender like :value2 "; params.put("value2", "%" + courseDetail.getGender().trim() + "%"); } if (courseDetail.getPkcourseDetail() != null && !courseDetail.getPkcourseDetail().equals("")) { if (hql.contains("where")) hql += " and "; else hql += " where "; hql += " t.pkcourseDetail like :value3 "; params.put("value3", "%" +
		 * courseDetail.getPkcourseDetail().toString() + "%"); }
		 */
		return hql;
	}

	@Override
	public void save(CourseDetail courseDetail) {
		TCourseDetail t = new TCourseDetail();
		BeanUtils.copyProperties(courseDetail, t);
		courseDetailDao.save(t);
	}

	@Override
	public void edit(CourseDetail courseDetail) {
		TCourseDetail t = new TCourseDetail();
		BeanUtils.copyProperties(courseDetail, t);
		courseDetailDao.saveOrUpdate(t);
	}

	@Override
	public void remove(CourseDetail courseDetail) {
		TCourseDetail t = courseDetailDao.get(TCourseDetail.class, courseDetail.getPkCourseDetail());
		courseDetailDao.delete(t);
	}

	@Override
	public Object getComboBox(CourseDetail courseDetail) {
		List<TCourseDetail> l = courseDetailDao.find(" from TCourseDetail");
		List<CourseDetail> pl = new ArrayList();
		if (l != null && l.size() > 0) {
			for (TCourseDetail t : l) {
				CourseDetail u = new CourseDetail();
				BeanUtils.copyProperties(t, u);
				pl.add(u);
			}
		}
		return pl;
	}

	public Date minADay(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DAY_OF_MONTH, -1);
		date = c.getTime();
		return date;
	}

	@Override
	public Object getCharts() {
		Charts c = new Charts();
		Date today = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			today = sdf.parse(sdf.format(today));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int g1 = groupCourseDao.count("from TGroupCourse t where t.beginDatetime> ? ", new Object[] { today });
		List<TGroupCourse> y1 = groupCourseDao.find("from TGroupCourse t where t.beginDatetime> ? ", new Object[] { today });
		int h1 = 0;
		for (TGroupCourse y : y1) {
			h1 = h1 + y.getTMembers().size();
		}
		int t1 = trainingDao.count("from TTraining t where t.beginDatetime> ? ", new Object[] { today });
		h1+=t1;
		
		Date m1 = minADay(today);
		int g2 = groupCourseDao.count("from TGroupCourse t where t.beginDatetime> ? and t.beginDatetime< ?", new Object[] { m1, today });
		List<TGroupCourse> y2 = groupCourseDao.find("from TGroupCourse t where t.beginDatetime> ? and t.beginDatetime< ?", new Object[] { m1, today });
		int h2 = 0;
		for (TGroupCourse y : y2) {
			h2 = h2 + y.getTMembers().size();
		}
		int t2 = trainingDao.count("from TTraining t where t.beginDatetime> ? and t.beginDatetime< ?", new Object[] { m1, today });
		h2+=t2;
		
		Date m2 = minADay(m1);
		int g3 = groupCourseDao.count("from TGroupCourse t where t.beginDatetime> ? and t.beginDatetime< ?", new Object[] { m2, m1 });
		List<TGroupCourse> y3 = groupCourseDao.find("from TGroupCourse t where t.beginDatetime> ? and t.beginDatetime< ?", new Object[] { m2, m1 });
		int h3 = 0;
		for (TGroupCourse y : y3) {
			h3 = h3 + y.getTMembers().size();
		}
		int t3 = trainingDao.count("from TTraining t where t.beginDatetime> ? and t.beginDatetime< ?", new Object[] { m2, m1 });
		h3+=t3;
		

		Date m3 = minADay(m2);
		int g4 = groupCourseDao.count("from TGroupCourse t where t.beginDatetime> ? and t.beginDatetime< ?", new Object[] { m3, m2 });
		List<TGroupCourse> y4 = groupCourseDao.find("from TGroupCourse t where t.beginDatetime> ? and t.beginDatetime< ?", new Object[] { m3, m2 });
		int h4 = 0;
		for (TGroupCourse y : y4) {
			h4 = h4 + y.getTMembers().size();
		}
		int t4 = trainingDao.count("from TTraining t where t.beginDatetime> ? and t.beginDatetime< ?", new Object[] { m3, m2 });
		h4+=t4;
		

		Date m4 = minADay(m3);
		int g5 = groupCourseDao.count("from TGroupCourse t where t.beginDatetime> ? and t.beginDatetime< ?", new Object[] { m4, m3 });
		List<TGroupCourse> y5 = groupCourseDao.find("from TGroupCourse t where t.beginDatetime> ? and t.beginDatetime< ?", new Object[] { m4, m3 });
		int h5 = 0;
		for (TGroupCourse y : y5) {
			h5 = h5 + y.getTMembers().size();
		}
		int t5 = trainingDao.count("from TTraining t where t.beginDatetime> ? and t.beginDatetime< ?", new Object[] { m4, m3 });
		h5+=t5;
		

		Date m5 = minADay(m4);
		int g6 = groupCourseDao.count("from TGroupCourse t where t.beginDatetime> ? and t.beginDatetime< ?", new Object[] { m5, m4 });
		List<TGroupCourse> y6 = groupCourseDao.find("from TGroupCourse t where t.beginDatetime> ? and t.beginDatetime< ?", new Object[] { m5, m4 });
		int h6 = 0;
		for (TGroupCourse y : y6) {
			h6 = h6 + y.getTMembers().size();
		}
		int t6 = trainingDao.count("from TTraining t where t.beginDatetime> ? and t.beginDatetime< ?", new Object[] { m5, m4 });
		h6+=t6;
		

		Date m6 = minADay(m5);
		int g7 = groupCourseDao.count("from TGroupCourse t where t.beginDatetime> ? and t.beginDatetime< ?", new Object[] { m6, m5 });
		List<TGroupCourse> y7 = groupCourseDao.find("from TGroupCourse t where t.beginDatetime> ? and t.beginDatetime< ?", new Object[] { m6, m5 });
		int h7 = 0;
		for (TGroupCourse y : y7) {
			h7 = h7 + y.getTMembers().size();
		}
		int t7 = trainingDao.count("from TTraining t where t.beginDatetime> ? and t.beginDatetime< ?", new Object[] { m6, m5 });
		h7+=t7;
		

		List<String> g = new ArrayList();
		List<String> t = new ArrayList();
		List<String> h = new ArrayList();
		List<String> x = new ArrayList();
		g.add(Integer.toString(g7));
		g.add(Integer.toString(g6));
		g.add(Integer.toString(g5));
		g.add(Integer.toString(g4));
		g.add(Integer.toString(g3));
		g.add(Integer.toString(g2));
		g.add(Integer.toString(g1));
		t.add(Integer.toString(t7));
		t.add(Integer.toString(t6));
		t.add(Integer.toString(t5));
		t.add(Integer.toString(t4));
		t.add(Integer.toString(t3));
		t.add(Integer.toString(t2));
		t.add(Integer.toString(t1));
		h.add(Integer.toString(h7));
		h.add(Integer.toString(h6));
		h.add(Integer.toString(h5));
		h.add(Integer.toString(h4));
		h.add(Integer.toString(h3));
		h.add(Integer.toString(h2));
		h.add(Integer.toString(h1));
		x.add(sdf.format(m6));
		x.add(sdf.format(m5));
		x.add(sdf.format(m4));
		x.add(sdf.format(m3));
		x.add(sdf.format(m2));
		x.add(sdf.format(m1));
		x.add(sdf.format(today));
		c.setX(x);
		c.setDetail(g);
		c.setDetail2(t);
		c.setDetail3(h);
		return c;
	}

}
